package com.fnzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fnzx.dao.DemandDao;
import com.fnzx.entity.DemandEntity;
import com.fnzx.entity.EnterpriseUserEntity;
import com.fnzx.entity.ProjectEntity;
import com.fnzx.entity.RelationEntity;
import com.fnzx.entity.vo.DemandVo;
import com.fnzx.service.DemandService;
import com.fnzx.service.EnterpriseUserService;
import com.fnzx.service.ProjectService;
import com.fnzx.service.RelationService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("demandService")
public class DemandServiceImpl extends ServiceImpl<DemandDao, DemandEntity> implements DemandService{
    @Resource
    private DemandDao demandDao;

    @Autowired
    ProjectService projectService;

    @Autowired
    RelationService relationService;
    @Autowired
    EnterpriseUserService enterpriseUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DemandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_created");
        IPage<DemandEntity> page = this.page(
          new Query<DemandEntity>().getPage(params),
          queryWrapper
        );
        List<DemandVo> demandVos = page.getRecords().stream().map(demandEntity -> {
            ProjectEntity projectEntity = projectService.getOne(new QueryWrapper<ProjectEntity>().eq("demand_id", demandEntity.getId()));
            RelationEntity relationEntity = relationService.getOne(new QueryWrapper<RelationEntity>().eq("id", demandEntity.getCategoryId()));
            EnterpriseUserEntity one = enterpriseUserService.getOne(new QueryWrapper<EnterpriseUserEntity>().eq("id", demandEntity.getEnterpriseId()));
            return new DemandVo(demandEntity,projectEntity,relationEntity,one);
        }).collect(Collectors.toList());
        IPage<DemandVo> result = new Page<>();
        BeanUtils.copyProperties(page,result);
        result.setRecords(demandVos);
        return new PageUtils(result);
    }


    @Override
    public List<ProjectEntity> getGuan(Long demandId) {
        List<ProjectEntity> project = demandDao.getGuan(demandId);
        return project;
    }

    @Override
    public List<RelationEntity> getFen() {
        return demandDao.getFen();
    }

    @Override
    public PageUtils queryPageByUser(Map<String, Object> params) {
        QueryWrapper<DemandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id",params.get("id"));
        queryWrapper.orderByDesc("gmt_created");
        IPage<DemandEntity> page = this.page(
                new Query<DemandEntity>().getPage(params),
                queryWrapper
        );
        List<DemandVo> demandVos = page.getRecords().stream().map(demandEntity -> {
            ProjectEntity projectEntity = projectService.getOne(new QueryWrapper<ProjectEntity>().eq("demand_id", demandEntity.getId()));
            RelationEntity relationEntity = relationService.getOne(new QueryWrapper<RelationEntity>().eq("id", demandEntity.getCategoryId()));
            EnterpriseUserEntity one = enterpriseUserService.getOne(new QueryWrapper<EnterpriseUserEntity>().eq("id", demandEntity.getEnterpriseId()));
            return new DemandVo(demandEntity,projectEntity,relationEntity,one);
        }).collect(Collectors.toList());
        IPage<DemandVo> result = new Page<>();
        BeanUtils.copyProperties(page,result);
        result.setRecords(demandVos);
        return new PageUtils(result);
    }


}
